<?php
include 'Mobile_Detect.php';
class Mobile_Detect_Parser extends Mobile_Detect{
    
    function __construct(){
        // Do nothing!
    }
    
    public function rulesToVarnishVCL(){
        $vcl = <<<VCL
# Sets X-Device to pc, mobile-phone or mobile-tablet based upon User Agent. 
# Based on script:  http://code.google.com/p/php-mobile-detect/
# Very basic implementation, needs work to cover all logic in above script
   
sub identify_device { 
  # Default to thinking it's a PC
  unset req.http.X-Device;
  set req.http.X-Device = "pc"; 

  # Known tablets
  if (
VCL;

        $rules = array();
        foreach ($this->tabletDevices as $key=>$regex) {
            $rules[] = 'req.http.User-Agent ~ "' . $regex . '"';   
        }
$vcl .= implode($rules, "\n\t || "); 
  
$vcl .= <<<VCL
) {
    set req.http.X-Device = "mobile-tablet";
  }

  # Known phones
  elsif (
VCL;
         $rules = array();
        foreach ($this->phoneDevices as $key=>$regex) {
            $rules[] = 'req.http.User-Agent ~ "' . $regex . '"';   
        }
$vcl .= implode($rules, "\n\t || "); 

$vcl .= <<<VCL
) {
    set req.http.X-Device = "mobile-phone"; 
  }

  
}
VCL;

        return $vcl;
    }
}


// Yeah thats right, I wrote some procedual code in the bottom of a class definition. Sew me.
$x = new Mobile_Detect_Parser();
$r = $x->rulesToVarnishVCL();

print_r($r);